package ws_server

import "time"

type ServerOptionsFunc func(opt *ServerOptions)

type AckType int

const (
	NoAck AckType = iota
	OnlyAck
	RigorAck
)

type ServerOptions struct {
	Authentication
	pattern           string
	maxConnectionIdle time.Duration

	ackType    AckType       // ack 模式
	ackTimeout time.Duration // ack 超时时间
}

func newServerOptions(f ...ServerOptionsFunc) ServerOptions {
	// 设置一个默认配置
	o := ServerOptions{
		Authentication:    new(authentication),         // 默认的鉴权中间件
		pattern:           "/ws",                       // 默认的websocket通信路由
		maxConnectionIdle: 0.5 * 60 * 60 * time.Second, // 默认的连接允许的最大空闲时间
		//ack:               NoAck,                       // 默认的ack模式
		ackTimeout: 15 * time.Second, // 默认的ack超时时间
	}

	// 如果有更改配置的函数传入，则进行配置更改
	for _, v := range f {
		v(&o)
	}
	return o
}

// WithServerAuthentication 更改Authentication配置
func WithServerAuthentication(auth Authentication) ServerOptionsFunc {
	return func(opt *ServerOptions) {
		opt.Authentication = auth
	}
}

// WithServerPattern 更改Pattern配置
func WithServerPattern(pattern string) ServerOptionsFunc {
	return func(opt *ServerOptions) {
		opt.pattern = pattern
	}
}

// WithServerMaxConnectionIdle 更改maxConnectionIdle配置
func WithServerMaxConnectionIdle(maxConnectionIdle time.Duration) ServerOptionsFunc {
	return func(opt *ServerOptions) {
		if maxConnectionIdle > 0 {
			opt.maxConnectionIdle = maxConnectionIdle
		}
	}
}

// WithServerAck 更改ack配置
func WithServerAck(ackType AckType) ServerOptionsFunc {
	return func(opt *ServerOptions) {
		opt.ackType = ackType
	}
}
